package com.bdqn.tour.servlet.Hqf;

import com.bdqn.tour.pojo.Scenic;
import com.bdqn.tour.service.HqfAddService;
import com.bdqn.tour.service.impl.hqfAddServiceImpl;
import com.bdqn.tour.util.MyConfig;

import javax.servlet.ServletException;
import javax.servlet.annotation.MultipartConfig;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.Part;
import java.io.File;
import java.io.IOException;
import java.util.UUID;

//添加景点
@WebServlet("/hqfAddScenic")
@MultipartConfig    //文件上传
public class HqfAddServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置编码格式
       req.setCharacterEncoding("UTF-8");
       resp.setCharacterEncoding("UTF-8");
       resp.setContentType("text/html;charset=UTF-8");
       //获取参数
       String scenicname = req.getParameter("scenicname");
       String scenicaddress = req.getParameter("address");
       String prices = req.getParameter("price");

       String satisfactions = req.getParameter("satisfaction");
        String city = req.getParameter("city");
        String html = req.getParameter("html");


        // 验证参数
        if (scenicname == null || scenicaddress == null || prices == null ||  satisfactions == null || city == null || html == null) {
            resp.getWriter().write("参数错误");
            return;
        }

        //转化整数类型
        int price = Integer.parseInt(prices);
        int satisfaction = Integer.parseInt(satisfactions);


        //处理图片上传
        String img = "";
        Part imgPart = req.getPart("picture");   //获取文件请求对象

        //限制文件大小
        long size = imgPart.getSize();
        if(size >0 && size <  5 * 1024 * 1024){
            String contentType = imgPart.getContentType();   //获取图片类型
            if(contentType.startsWith("image")){    //startwith图片image开头

                String fileName = imgPart.getSubmittedFileName();   //返回文件名
                //获取文件后缀
                String fileSuffix = fileName.substring(fileName.lastIndexOf("."));
                //新文件名  自行定义
                String newFileName = UUID.randomUUID() + fileSuffix;
                //创建文件对象
                String filePath = MyConfig.getValue("filePath");
                try {
                    imgPart.write(filePath + File.separatorChar + newFileName);
                    img = newFileName;
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }else{
                System.out.println("imgPart:上传的不是图片格式");
            }
        }else{
            System.out.println("imgPart:不符合大小");
        }
        //创建对象scenic
        Scenic scenic = new Scenic();
        scenic.setScenic_name(scenicname);
        scenic.setScenic_address(scenicaddress);
        scenic.setPrice(price);
        scenic.setImg(img);
        scenic.setSatisfaction(satisfaction);
        scenic.setCity(city);
        scenic.setHtml(html);
        //业务层hqfAddService,使用hqfAddServiceImpl类进行实现
        HqfAddService service = new hqfAddServiceImpl();
       int count =  service.addScenic(scenic);
        if(count >0){
            System.out.println("添加成功");
            //转发到查询页面hqfSearch
            req.getRequestDispatcher("/hqfSearch").forward(req,resp);
        }else{
            resp.getWriter().write("添加失败");
        }
        }
    }

